package br.com.ca5.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import br.com.ca5.dao.MenuDao;
import br.com.ca5.entity.Menu;
import br.com.ca5.entity.Usuario;

@Repository
public class MenuDaoImpl extends HibernateDaoSupport implements MenuDao{
	private static final Log log = LogFactory.getLog(MenuDaoImpl.class);
	// property constants
	public static final String NOME = "nome";
	public static final String USUARIO_ID = "usuarioId";

	protected void initDao() {
		// do nothing
	}
	
	public List<Menu> buscaMenusPaiPorUsuario(Usuario usuario) {
		List<Menu> listMenu;
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(Menu.class,"m");
			criteria.createAlias("m.usuario", "u");
			criteria.add(Restrictions.eq("u.id", usuario.getId()));
			criteria.add(Restrictions.isNull("m.menuPai"));
			
			listMenu = getHibernateTemplate().findByCriteria(criteria);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return listMenu;
	}
	
	public List<Menu> buscaMenusFilhosPorUsuario(Usuario usuario, Menu menuPai) {
		List<Menu> listMenu;
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(Menu.class,"m");
			criteria.createAlias("m.usuario", "u");
			criteria.add(Restrictions.eq("u.id", usuario.getId()));
			criteria.add(Restrictions.eq("m.menuPai", menuPai));
			
			listMenu = getHibernateTemplate().findByCriteria(criteria);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return listMenu;
	}

	public void save(Menu transientInstance) {
		log.debug("saving Menu instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Menu persistentInstance) {
		log.debug("deleting Menu instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Menu findById(java.lang.Long id) {
		log.debug("getting Menu instance with id: " + id);
		try {
			Menu instance = (Menu) getHibernateTemplate().get(
					"br.com.ca5.dao.impl.Menu", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Menu instance) {
		log.debug("finding Menu instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Menu instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Menu as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByNome(Object nome) {
		return findByProperty(NOME, nome);
	}

	public List findByUsuarioId(Object usuarioId) {
		return findByProperty(USUARIO_ID, usuarioId);
	}

	public List findAll() {
		log.debug("finding all Menu instances");
		try {
			String queryString = "from Menu";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Menu merge(Menu detachedInstance) {
		log.debug("merging Menu instance");
		try {
			Menu result = (Menu) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Menu instance) {
		log.debug("attaching dirty Menu instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Menu instance) {
		log.debug("attaching clean Menu instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static MenuDaoImpl getFromApplicationContext(ApplicationContext ctx) {
		return (MenuDaoImpl) ctx.getBean("MenuDAO");
	}
	
}